// Yang Shuai  Copyright (c) 2022 https://yangbuyi.top.
// Copyright (c) 2022. Yangbuyi, personal projects are not allowed to be commercialized without permission.
// Please keep the information of the original author of the code. Thank you

package top.yangbuyi.factory;

import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * 引擎工厂
 * @author yangbuyi.top
 */
@Slf4j
public class FaceEngineFactory extends BasePooledObjectFactory<FaceEngine> {

    private final String appId;
    private final String sdkKey;
    private final String sdkLibPath;
    private final EngineConfiguration engineConfiguration;
    private final Integer detectFaceMaxNum = 10;
    private final Integer detectFaceScaleVal = 16;
    private final DetectMode detectMode = DetectMode.ASF_DETECT_MODE_IMAGE;
    private final DetectMode detectVideo = DetectMode.ASF_DETECT_MODE_VIDEO;
    private final DetectOrient detectFaceOrientPriority = DetectOrient.ASF_OP_0_ONLY;


    public FaceEngineFactory (String sdkLibPath, String appId, String sdkKey, EngineConfiguration engineConfiguration) {
        this.sdkLibPath = sdkLibPath;
        this.appId = appId;
        this.sdkKey = sdkKey;
        // 引擎配置类
        this.engineConfiguration = engineConfiguration;

    }


    @Override
    public FaceEngine create () throws Exception {
        FaceEngine faceEngine = new FaceEngine(sdkLibPath);
        // 用于在线激活SDK---
        int activeCode = faceEngine.activeOnline(appId, sdkKey);
        log.info("在线激活SDK完毕！,{}", activeCode);
        int initCode = faceEngine.init(engineConfiguration);
        log.info("初始化功能引擎完毕！,{}", initCode);
        return faceEngine;
    }

    @Override
    public PooledObject<FaceEngine> wrap (FaceEngine faceEngine) {
        return new DefaultPooledObject<>(faceEngine);
    }


    @Override
    public void destroyObject (PooledObject<FaceEngine> p) throws Exception {
        FaceEngine faceEngine = p.getObject();
        int unInitCode = faceEngine.unInit();
        super.destroyObject(p);
        log.info("销毁对象完毕! faceEngineUnInitCode: {}", unInitCode);
    }
}
